import { showNotification } from '@/utils/notify';
import request from '@/utils/request';
import { defineStore } from 'pinia';
import { ref } from 'vue';

type Teacher = {    
        name: string,
        staffNumber: string,
        gender: string,
        department: string,
        title: string
       
}

type TeachersPage = {
    code: number,
  message: string, 
  data: {
    records: Teacher[],
    total: number,
    size: number,
    current: number,
    orders: [],
    optimizeCountSql: boolean,
    hitCount: boolean,
    "countId": null,
    "maxLimit": null,
    searchCount: boolean,
    pages: number
  }
}


export const useTeacherStore = defineStore('teacher', () => {
    const teachers = ref<Teacher[]>([]);
    const total = ref(0); // 总教师数量
    const currentPage = ref(1); // 当前页码
    const pageSize = ref(10); // 每页显示的教师数量

    const fetchTeachers = async () => {
        try {
            const { data } = await request.get<TeachersPage>(
                `/api/teachers/page?current=${currentPage.value}&size=${pageSize.value}`
            );
            
            teachers.value = data.records;
            total.value = data.total;
        } catch (error) {
            showNotification("获取课程列表失败", true);
        }
    };

    const addTeacher = async (teacher: Teacher) => {
        const { code, data } = await request.post<{ code: number; data: object }>('/api/teachers/add', teacher);
        if (code === 200) {
            showNotification("教师添加成功", false);
            fetchTeachers()

        } else {
            showNotification("课程添加失败", true);
        }
    };

    const updateTeacher = async (index: number, updatedTeacher: Teacher) => {
        try {
            const staffNumber = updatedTeacher.staffNumber;
            const { code, data } = await request.post<{ code: number; data: object }>(
                `/api/teachers/update/${staffNumber}`,
                updatedTeacher
            );
            if (code === 200) {
                showNotification("教师更新成功", false);
            fetchTeachers()

            } else {
                showNotification("教师更新失败", true);
            }
        } catch (error) {
            showNotification("教师更新失败", true);
        }
        
    };

    const deleteTeacher = async (index: number) => {
        try {
            const staffNumber = teachers.value[index].staffNumber;
            const { code, data } = await request.post<{ code: number; data: object }>(
                `/api/teachers/remove/${staffNumber}`
            );
            if (code === 200) {
                showNotification("教师删除成功", false);
            } else {
               showNotification("教师删除失败", true);
            }
            fetchTeachers()

        } catch (error) {
            showNotification("教师删除失败", true);
        }
    };

    const changePage = (page: number) => {
        currentPage.value = page; // 更新当前页码
        fetchTeachers(); // 重新获取课程列表
    };

    onMounted(() => {
        fetchTeachers(); // 页面加载时获取课程列表 
    })
    return {
        teachers,
        addTeacher,
        updateTeacher,
        deleteTeacher,
        changePage,
        currentPage,
        pageSize,
        total
    };
});